package com.tarena.lbs.business.controller;

import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.dao.mapper.BusinessMapper;
import com.tarena.lbs.business.pojo.param.BusinessParam;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import com.tarena.lbs.business.pojo.vo.BusinessStoreVO;
import com.tarena.lbs.business.pojo.vo.BusinessVO;
import com.tarena.lbs.business.pojo.vo.StoreChainVO;
import com.tarena.lbs.business.servcie.BusinessService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理和商家相关的接口功能
 */
@Slf4j
@RestController
@Tag(name = "商家相关功能接口")
public class BusinessController {
    @Autowired
    private BusinessService businessService;
    @Autowired
    private BusinessMapper businessMapper;

    //商家的分页列表查询
    //读数据入参 单个属性 id name 对象数据 query结尾
    @Operation(summary = "查询商家列表")
    @GetMapping("/admin/basic/business/info/list")
    public Result<PageResult<BusinessVO>> pageList(BusinessQuery query)
            throws BusinessException {
        PageResult<BusinessVO> voPage = businessService.pageList(query);
        return new Result<>(voPage);
    }

    //表单填写完毕 新增提交商家数据保存
    //form请求表单格式参数 key=value&key=value
    //请求体携带json {"key","value"}
    //lbs智慧营销项目中 写操作 如果多个属性 命名类型Param后缀
    @Operation(summary = "新增商家")
    @PostMapping("/admin/basic/business/info/save")
    public Result<Void> save(@RequestBody BusinessParam param) throws BusinessException {

        System.out.println(businessMapper);
        businessService.save(param);

        return Result.success();//code=0 success=true
    }

    //商家的业务中 活动查询商家 和旗下店铺列表封装
    @Operation(summary = "商家详情")
    @GetMapping("/admin/basic/business/info/detail/{id}")
    public Result<BusinessStoreVO> busiStoreDetail(@PathVariable("id") Integer businessId)
            throws BusinessException {
        BusinessStoreVO vo = businessService.busiStoreDetail(businessId);
        return new Result<>(vo);
    }

    @PostMapping("/admin/basic/chain/getAll")
    public Result<PageResult<StoreChainVO>> getAll(Integer pageNo, Integer pageSize)
            throws BusinessException {
        List<StoreChainVO> storeChainVOS = businessService.getAll();
        PageResult<StoreChainVO> storeChainVOPageResult = new PageResult<>();
        storeChainVOPageResult.setPageNo(1);
        storeChainVOPageResult.setPageSize(10);
        storeChainVOPageResult.setObjects(storeChainVOS);
        storeChainVOPageResult.setTotal((long) storeChainVOS.size());
        return new Result<>(storeChainVOPageResult);
    }

    @Operation(summary = "更改二维码状态为启用")
    @PutMapping("/admin/basic/chain/enable/{id}")
    public Result<Void> enable(@PathVariable Integer id)
            throws BusinessException {
        businessService.updateStatus(id);
        return Result.success();
    }

    @Operation(summary = "更改二维码状态为禁用")
    @PutMapping("/admin/basic/chain/disable/{id}")
    public Result<Void> disable(@PathVariable Integer id)
            throws BusinessException {
        businessService.updateStatus(id);
        return Result.success();
    }
}




















